package com.example.demo.service.excel.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.handler.inter.IReadHandler;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.analysis.ExcelReadExecutor;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.example.demo.entity.CollectionName;
import com.example.demo.service.excel.ExcelService;
import com.example.demo.utils.convert.ConvertUtil;
import com.example.demo.utils.excel.ExcelListener;
import com.mongodb.client.MongoClient;
import com.monitorjbl.xlsx.StreamingReader;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;

import org.springframework.beans.BeanUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

/**
 * @description: excel
 * @author: cj
 * @create: 16-03-08 16:08:10
 */
@Slf4j
@Service
public class ExcelServiceImpl implements ExcelService {
    private List<Map<String, Object>> list = new ArrayList<>();
    @Resource
    MongoTemplate mongoTemplate;
    private static MongoClient mongoClient;

    @Override
    public boolean inc(MultipartFile multipartFile, String collectionName) {
        list.clear();
        int count = 0;
        try {
            Map<Integer, String> fileMap = new HashMap<>( 30 );
            long size = multipartFile.getSize();
            InputStream in = multipartFile.getInputStream();
            File file = new File( multipartFile.getOriginalFilename() );
            long length = file.length();
            log.info( "size:" + ConvertUtil.getPrintSize( size ) );
            log.info( "length:" + length );
            Workbook wk = StreamingReader.builder()
                    .rowCacheSize( 100 )  //缓存到内存中的行数，默认是10
                    .bufferSize( 4096 )  //读取资源时，缓存到内存的字节大小，默认是1024
                    .open( in );  //打开资源，必须，可以是InputStream或者是File，注意：只能打开XLSX格式的文件
            Sheet sheet = wk.getSheetAt( 0 );
            long l = System.nanoTime();
            log.info( l + "" );
            //遍历所有的行

            int firstRowNum = sheet.getFirstRowNum();
            System.out.println();
            for (Row row : sheet) {
                count++;
                int physicalNumberOfCells = row.getPhysicalNumberOfCells();
                if (row.getRowNum() == 0) {
                    for (int i = 0; i < physicalNumberOfCells; i++) {
                        fileMap.put( i, row.getCell( i ).getStringCellValue() );
                    }
                } else {
                    System.out.println( "开始遍历第" + row.getRowNum() + "行数据：" );
                    HashMap<String, Object> map = new HashMap<String, Object>( 15 );
                    //遍历所有的列
                    if (row.getRowNum() == 12) {
                        log.info( "" );
                    }
                    for (int i = 0; i < physicalNumberOfCells; i++) {
                        String stringCellValue = StringUtils.isEmpty( row.getCell( i ) ) ? "" : row.getCell( i ).getStringCellValue();
                        map.put( fileMap.get( i ), stringCellValue );
                    }
                    list.add( map );
                }
                if (list.size() > 2000) {
                    mongoTemplate.insert( list, CollectionName.GOODS );
                    list.clear();
                } else {
                    if (count == firstRowNum) {
                        mongoTemplate.insert( list, CollectionName.GOODS );
                        list.clear();
                    }
                }
            }
            log.info( (System.nanoTime() - l) + "" );
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    private void test(MultipartFile multipartFile, InputStream in) {
        //使用api获取到的List数据
        try {
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        List<Map<String, Object>> list = new ArrayList<>();
        list.add( null );
    }
}
